<?php
// 应用公共文件

use app\common\service\AuthService;
use think\facade\Cache;

use EasyAdmin\upload\driver\txcos\Cos;

//腾讯云上传图片
function txcossc($path,$datapath){
    $upload = Cos::instance(sysconfig('upload'))
        ->save($path, $datapath);
    if($upload){
        unlink($datapath);
        return $upload;
    }
}



/**
 *PHP stdClass Object转array
 *
 * @return array
 */
function objectToArray($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = objectToArray($value);
        }
    }
    return $array;
}


/**
 *json固定格式1
 */
function outputJson($code, $message = '', $content = null)
{
    $data['code'] = $code;
    $data['message'] = $message ? $message : lang('code_' . $code);
    $data['data'] = $content;
    return json_encode($data);
}

function getDirContent($path){
    if(!is_dir($path)){
        return false;
    }
    $arr = array();
    $data = scandir($path);
    foreach ($data as $value){
        if($value != '.' && $value != '..'){
            $arr[] = $value;
        }
    }
    return $arr;
}

/**
 * 二维数组排序；
 * @param string $prefix 前缀
 * @param array $arr 排序数组
 * @param array $shortKey 排序字段
 * @param array $short 排序方式
 * @param array $shortType 排序类型
 * @return  array
 */
function multi_array_sort($arr, $shortKey, $short = SORT_DESC, $shortType = SORT_REGULAR)
{
    foreach ($arr as $key => $data) {
        $name[$key] = $data[$shortKey];
    }
    array_multisort($name, $shortType, $short, $arr);
    return $arr;
}

function curlGet($url, $timeOut = 5, $aHeader = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'), $aHeader));
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);          //单位 秒，也可以使用
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        $data = curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

/**
 * post请求
 *
 * @param [type] $url
 * @param [type] $data
 * @return void
 */
function curlPost($url, $data, $timeOut = 5, $aHeader = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(array('Content-Type:application/json'), $aHeader));
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);          //单位 秒，也可以使用
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        $data = curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

if (!function_exists('__url')) {

    /**
     * 构建URL地址
     * @param string $url
     * @param array $vars
     * @param bool $suffix
     * @param bool $domain
     * @return string
     */
    function __url(string $url = '', array $vars = [], $suffix = true, $domain = false)
    {
        return url($url, $vars, $suffix, $domain)->build();
    }
}

if (!function_exists('password')) {

    /**
     * 密码加密算法
     * @param $value 需要加密的值
     * @param $type  加密类型，默认为md5 （md5, hash）
     * @return mixed
     */
    function password($value)
    {
        $value = sha1('blog_') . md5($value) . md5('_encrypt') . sha1($value);
        return sha1($value);
    }

}

if (!function_exists('xdebug')) {

    /**
     * debug调试
     * @param string|array $data 打印信息
     * @param string $type 类型
     * @param string $suffix 文件后缀名
     * @param bool $force
     * @param null $file
     */
    function xdebug($data, $type = 'xdebug', $suffix = null, $force = false, $file = null)
    {
        !is_dir(runtime_path() . 'xdebug/') && mkdir(runtime_path() . 'xdebug/');
        if (is_null($file)) {
            $file = is_null($suffix) ? runtime_path() . 'xdebug/' . date('Ymd') . '.txt' : runtime_path() . 'xdebug/' . date('Ymd') . "_{$suffix}" . '.txt';
        }
        file_put_contents($file, "[" . date('Y-m-d H:i:s') . "] " . "========================= {$type} ===========================" . PHP_EOL, FILE_APPEND);
        $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . PHP_EOL;
        $force ? file_put_contents($file, $str) : file_put_contents($file, $str, FILE_APPEND);
    }
}

if (!function_exists('sysconfig')) {

    /**
     * 获取系统配置信息
     * @param $group
     * @param null $name
     * @return array|mixed
     */
    function sysconfig($group, $name = null)
    {
        $where = ['group' => $group];
        $value = empty($name) ? Cache::get("sysconfig_{$group}") : Cache::get("sysconfig_{$group}_{$name}");
        if (empty($value)) {
            if (!empty($name)) {
                $where['name'] = $name;
                $value = \app\admin\model\SystemConfig::where($where)->value('value');
                Cache::tag('sysconfig')->set("sysconfig_{$group}_{$name}", $value, 3600);
            } else {
                $value = \app\admin\model\SystemConfig::where($where)->column('value', 'name');
                Cache::tag('sysconfig')->set("sysconfig_{$group}", $value, 3600);
            }
        }
        return $value;
    }
}

if (!function_exists('array_format_key')) {

    /**
     * 二位数组重新组合数据
     * @param $array
     * @param $key
     * @return array
     */
    function array_format_key($array, $key)
    {
        $newArray = [];
        foreach ($array as $vo) {
            $newArray[$vo[$key]] = $vo;
        }
        return $newArray;
    }

}

if (!function_exists('auth')) {

    /**
     * auth权限验证
     * @param $node
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function auth($node = null)
    {
        $authService = new AuthService(session('admin.id'));
        $check = $authService->checkNode($node);
        return $check;
    }

}